Java Basics _ Inner classes: In Java, a class can be defined inside another class or inside a method, such that a class is called an inner class.The primary role of
this class, and become the anonymous inner class.2, you will want to define a callback function, but do not want to write a lot of code, you can consider anonymous inner class.3. Syntax:Class outerclass{ //Peripheral class fie
external class, which needs to be accessed by an object-level object.You cannot declare any static member in a non-static inner class.Inner classes can be called to each other, for example: access to member-internal classes1 class A {2 // B, C can call each other 3 class
class ' Demo { public static void Main (string[] args) { out.in in = new Out (). New in (); In.print (); or Access/ * out with sowing = new Out (); Out.in in = Out.new in (); In.print (); */ }}Run Result: 12From the above example, it is not difficult to see that the inner
static data, a static method, or another static inner class (inner classes may be nested more than one layer). However, the static inner class can have all of them. This is the second difference between two people. Local inner cl
class reference to the subclass instance of new, so what happens to the interface?Example 3: Anonymous class implementation interfaceFirst define an interface public interface Human {public void eat ();p ublic void Drink ();}public class Unnamedclass {public static void main (string[] args) { As you can
interfaceEquivalent to an implementation class object that defines the interface and overrides all the abstract methods in the interfaceSportable s=new sportable () {public void Sport () {System.out.println ("playing basketball");}};S.sport ();System.out.println (S.getclass ());//class com.aowin.noname.test$3}}Entity classClass person{public void Eat () {System.out.println ("eat");}}
used (can be removed final)//inner Inner = new Inner (), cannot be instantiated here, because the Inner class has not been initialized, you cannot call that class inner{// Local
Programming | programs | inheriting | Examples about inheritance internal class--java programming thought sample program analysis;
Class Egg2 {
Protected class Yolk {
Public yolk () {
System.out.println ("Egg2.yolk ()");
}
public void F () {
System.out.println ("Egg2.yolk.f ()");
}
}
Private yolk y = new yolk ();
Publi
its own properties and methods, but it cannot be instantiated because the parent class is instantiated, The parent class does not change the properties and methods in the anonymous class private String name = "AAA"; The {///anonymous class can have code blocks initialized. Cannot use static code blocks such as: Static
1. A Java program is a collection of classes.A class in a program has two kinds of arrangement structures.
For example: classenclosingclass{.....
Class anestedclass{......}
}
2. In some special cases, a class is also placed within the internal definition of another
= Outter.getinnerclassinstance (); INNER1.DRAWSAHPE (); }}Internal classes can have private access, protected access, public access, and package access. For example, if the member inner class inner with private decoration, it can only be accessed inside the external class
restricts access to the external classes. As an inner class of tools, the general access modifier is public or default. For ease of use, Java allows direct access to the members of the host class within the inner class (which a
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.